package com.poisonh.unifiedauth.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lzy.okhttputils.model.HttpParams;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.model.BusinessCard;
import com.poisonh.unifiedauth.ui.svprogresshud.SVProgressHUD;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.MyHttpsUtils;
import org.json.JSONException;
import org.json.JSONObject;

public class CardetailsActivity extends BaseActivity implements View.OnClickListener {

    private String bUerId;//被扫描用户的id
    private TextView car_name;
    private TextView cellphone;
    private TextView company_name;
    private TextView email_text;
    private ImageView head_img;
    private Button button_delete;
    private int list_location;
    @Override
    protected void loadContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_cardetails);
        initTitleView();
        initView();
        //SVProgressHUD.showWithStatus(this, "加载中...");
        QueryCardInfo();
    }

    @Override
    protected void initView() {
        mRightBtn.setVisibility(View.GONE);
        setTitleTxt("详情");
        mLeftBtn.setOnClickListener(this);

        BusinessCard  businessCard= (BusinessCard) getIntent().getSerializableExtra("BusinessCard");
        bUerId=businessCard.getUid();

        car_name= (TextView) findViewById(R.id.car_name);//姓名
        cellphone= (TextView) findViewById(R.id.cellphone);//手机号
        company_name= (TextView) findViewById(R.id.company_name);//公司名称
        email_text= (TextView) findViewById(R.id.email_text);
        head_img= (ImageView) findViewById(R.id.head_img);

        button_delete= (Button) findViewById(R.id.button_delete);//删除按钮
        button_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                list_location=getIntent().getIntExtra("list_location",-1);
                AlertDialog.Builder builder = new AlertDialog.Builder(CardetailsActivity.this);
                builder.setMessage("确定删除?");
                builder.setTitle("提示");
//
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        deleteCardInfo(bUerId);
//                        Toast.makeText(CardetailsActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
//                        finish();
                    }
                });

                //添加AlertDialog.Builder对象的setNegativeButton()方法
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {


                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

                builder.create().show();
            }


        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.ib_left_btn:
                finish();
                break;

        }
    }




    /**
     * 查询用户名片详细信息
     */
    public void QueryCardInfo() {

        HttpParams params = new HttpParams();
        params.put("token", mStrUserToken);
        //扫描用户id
        params.put("userId", mStrUserId);
        //被扫描用户id
          params.put("destId", bUerId);
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.CARDCheckdetailsInfo, params, new MyHttpsUtils.ResultCallback() {
            @Override
            public void onSuccess(String response) {
                Log.e("Resonpse", "onSuccess: "+response.toString());
                try {
                    JSONObject object = new JSONObject(response);
                    String code = object.getString("code");
                    if ("200".equals(code)) {
                        String object1 = object.getString("results");
                        Gson gson=new Gson();
                        BusinessCard businessCard=gson.fromJson(object1,BusinessCard.class);
                        String company =businessCard.getCompany();//公司名称
                        String name =businessCard.getName();//姓名 昵称
                        String email =businessCard.getEmail();//email
                        String mobile  =businessCard.getMobile();//手机
                        String headImg  =businessCard.getHeadImg();//头像

                        car_name.setText(name);
                        cellphone.setText(mobile);
                        company_name.setText(company);
                        email_text.setText(email);
                        Glide.with(CardetailsActivity.this).load(ConstantUitls.REQUEDT_IMG_URL +headImg).into(head_img);
                        SVProgressHUD.dismiss(CardetailsActivity.this);//加载进度消失
                        Toast.makeText(CardetailsActivity.this,"加载成功",Toast.LENGTH_SHORT).show();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    SVProgressHUD.dismiss(CardetailsActivity.this);
                    Toast.makeText(CardetailsActivity.this,"加载失败",Toast.LENGTH_SHORT).show();
                }
                Log.d("AAA", response);
            }

            @Override
            public void onFailure(String failReason)

            {
                Log.d("AAA", failReason);
                SVProgressHUD.dismiss(CardetailsActivity.this);
                Toast.makeText(CardetailsActivity.this,"加载失败",Toast.LENGTH_SHORT).show();
            }
        });
    }


    /**
     * 删除添加的用户名片信息
     */
    public void deleteCardInfo(String delete_B_uid) {

        HttpParams params = new HttpParams();
        params.put("token", mStrUserToken);
        //扫描用户id
        params.put("userId", mStrUserId);
        //被扫描用户id
        params.put("destId", delete_B_uid);
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.CARDDELETEUSER, params, new MyHttpsUtils.ResultCallback() {
            @Override
            public void onSuccess(String response) {
                Log.e("Resonpse", "onSuccess: "+response.toString());
                try {
                    JSONObject object = new JSONObject(response);
                    String code = object.getString("code");
                    if ("200".equals(code)) {
                        Toast.makeText(CardetailsActivity.this,"删除成功",Toast.LENGTH_SHORT).show();


                        Intent intent=new Intent();
                        intent.putExtra("delete_location",list_location);
                        setResult(0,intent);
                        finish();
                    }else if("201".equals("code")){
                        Toast.makeText(CardetailsActivity.this,"删除失败",Toast.LENGTH_SHORT).show();
                    }

                } catch (JSONException e) {
                    Toast.makeText(CardetailsActivity.this,"删除失败",Toast.LENGTH_SHORT).show();
                    e.printStackTrace();

                }
                Log.d("AAA", response);
            }

            @Override
            public void onFailure(String failReason)

            {
                Log.d("AAA", failReason);
            }
        });
    }


}



